<?php
include 'includes/application_top.inc';
include 'includes/paginate.php';
include 'auth.php';
?>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta name="description" content="Easy Living - Responsive Real Estate Template">
		<meta name="keywords" content="Themes, real estate, responsive, themeforest, Templates">
		<meta name="author" content="Rype Pixel [Chris Gipple]">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>Welcome Listing grid</title>
		<!-- html5 support in IE8 and later -->
		<script src="../../html5shiv.googlecode.com/svn/trunk/html5.js"></script>
		<!-- CSS file links -->
		<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
		<link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
		<link href="css/styles.css" rel="stylesheet" type="text/css" media="all" />
		<link href="css/responsive.css" rel="stylesheet" type="text/css" media="all" />
	</head>
	<body>
		<!-- Start Header -->
		<?php
			include 'includes/header.php';
		?>
		<!-- End Header -->
		<!-- start subheader -->
		<p>
			&nbsp;
		</p>
		<!-- start recent properties -->
		<section class="properties">
			<div class="container">
				<?php
				if(isset($_GET['Msg'])){
					$Msg=$_GET['Msg'];
					echo success("$Msg");
				}
				// Delete URL
				if (isset($_GET['ac'])) {
					$ac = $_GET['ac'];
					if ($ac == 'Delete') {
						$id = $_GET['id'];
						$sql = mysql_query("DELETE FROM shorturl WHERE id='$id'") or die(mysql_error());
						echo success("Successfully Deleted");
					}
				}

				include 'includes/left.php';
			?>

				<div class="row">
					<div class="col-sm-6">
						<div class="propertyItem">
							<!--  onsubmit="validate();"-->
							<?php  $user_id = $_SESSION['u_id'];
$RefAuthid = 2;
$authCheck  = authentication($RefAuthid,$user_id);
if($authCheck==1)
{
							?>

							<?php 	$search = trim(@$_REQUEST['search']); ?>
							<form name="frm1" id="frm1" action="listing_grid.php" method="post">
							<table border="1" class="propertyDetails">
							<tr>
							<td>Search by Url</td>
							<td><input type="text" name="search" id="search" value=""> </td>
							<td><input type="submit" name="submit" value="Submit" /><a href="listing_grid.php">Clear All</a></td>
							</tr>
							<?php 
							if($search!=''){
							
							echo '<tr><td colspan="5">Search by: ' . $search . '</td></tr>'; }?>
							</table>
							</form>

							<table border="1" class="propertyDetails">
							<tr class="tabHead">
							<td>Short Url</td>
							<td>Long Url</td>
							<td>Date</td>
							<td>Total hit</td>
							<td colspan="2">Tools</td>
							</tr>
							<?php

							$per_page = 5;
							if ($search != "") {
								$search = str_replace(ROOTURL, '', $search);
								$select = "SELECT * FROM `shorturl` WHERE `shorturl_code` LIKE '%$search%' OR `long_url` LIKE '%$search%'  ORDER BY id DESC";

							} else {
								$select = "Select * From shorturl ORDER BY id DESC";
							}

							$result = mysql_query($select);
							$total_results = mysql_num_rows($result);
							$pagesUrl = "tpages=" . @$_REQUEST['tpages'] . "&page=" . @$_REQUEST['page'];
							$total_pages = ceil($total_results / $per_page);
							//total pages we going to have
							//-------------if page is setcheck------------------//
							$show_page = 1;
							if (isset($_GET['page'])) {
								$show_page = $_GET['page'];
								//it will telles the current page
								if ($show_page > 0 && $show_page <= $total_pages) {
									$start = ($show_page - 1) * $per_page;
									$end = $start + $per_page;
								} else {
									// error - show first set of results
									$start = 0;
									$end = $per_page;
								}
							} else {
								// if page isn't set, show first set of results
								$start = 0;
								$end = $per_page;
							}
							// display pagination
							$page = intval(@$_GET['page']);

							$tpages = $total_pages;
							if ($page <= 0)
								$page = 1;

							$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;

							echo '<div class="pagination"><ul>';
							if ($total_pages > 1) {
								echo paginate($reload, $show_page, $total_pages);
							}
							echo "</ul></div>";

							if ($total_results > 0) {
								for ($i = $start; $i < $end; $i++) {
									// make sure that PHP doesn't try to show results that don't exist
									if ($i == $total_results) {
										break;
									}
									$id = mysql_result($result, $i, 'id');
									$long_url = mysql_result($result, $i, 'long_url');
									$shorturl_code = ROOTURL . mysql_result($result, $i, 'shorturl_code');
									// echo out the contents of each row into a table
									echo '<tr class="propertyDetails1">';
									echo '<td><a target="_blank" href="' . $shorturl_code . '">' . $shorturl_code . '</a></td>';
									echo '<td>' . $long_url . '</td>';
									echo '<td>' . mysql_result($result, $i, 'create_datetime') . '</td>';
									echo '<td>' . hitcount($id) . '</td>';
									echo '<td><a href="shorturl.php?ac=Edit&' . $pagesUrl . '&id=' . $id . '&url=' . $long_url . '">edit</a></td>';
									echo '<td><a onClick="return confirm(\'Are you sure to delete?\')" href="listing_grid.php?ac=Delete&' . $pagesUrl . '&id=' . $id . '&url=' . $long_url . '">Delete</a></td>';
									/* echo '<td><input type="submit" value="Delete" onclick="redirecttest('.$id.');" />,
									 <a href="shorturl.php?ac=Edit&'.$pagesUrl.'&id='.$id.'&url='.$long_url.'">edit</a>,
									 <a href="listing_grid_details.php?ac=DETAILS&'.$pagesUrl.'&id='.$id.'">details</a></td>';*/
									echo "</tr>";
								}
							} else { echo '<tr><td colspan="5">NO Records Found</td></tr>';
							}
						?>
							</table>

							<?php
							}else { echo '
							<p align="center">
							</br></br></br><b>You have been denied to access.</b></br></br></br></br></br></br></br></br></br></br></br></br>
							</p>';}
	?>
							<hr />
						</div>
					</div>
				</div><!-- end row -->
			</div><!-- end container -->
		</section>
		<!-- end recent properties -->
		<div class="bottomBar">
			<div class="container">
				<p>
					&nbsp;
				</p>
			</div>
		</div>

		<!-- JavaScript file links -->
		<script src="js/jquery.js"></script>
		<!-- Jquery -->
		<script src="js/bootstrap.min.js"></script>
		<!-- bootstrap 3.0 -->
		<script src="js/respond.js"></script>

		<script type="text/javascript">
			function redirecttest (argument) {
<!--
var retVal = confirm("Do you want to delete ?");
if( retVal == true ){
//      alert("User wants to continue!");
window.location="action.php?URE=DELETE&<?php echo $pagesUrl; ?>
	&id="+argument;
	return true;
	}else{
	//  alert("User does not want to continue!");
	return false;
	}
	//-->
	}

		</script>

		<script type="text/javascript">
			function validate() {
				var url = document.getElementById("url").value;
				var pattern = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
				if (pattern.test(url)) {
					alert("Url is valid");
					return true;
				} else {
					alert("Url is not valid!");
					return false;

				}

			}
		</script>
	</body>
</html>